const app = getApp();
const apiPrefix = app.globalData.apiPrefix;
import { setShare, regPhone } from "../../utils/util";

Page({
  data: {
    list: [],

    // 分页
    page: 1,
    total: 0,
    limit: 4,
    isMore: true,
    loaded: false,
    allList: [],

    // 合作
    concactShow: false,
    phone: "",
    name: "",
    content: "",

    dialogStyls: "top: 0 !important; z-index: 999999; ",

    bottom: 0,
  },
  onLoad() {
    this.getList();

    setShare();
  },

  // 列表
  getList(params = {}) {
    wx.showLoading();
    const that = this;

    const { limit } = this.data;
    const { page = 1 } = params;

    that.setData({
      loaded: false,
      page,
    });

    wx.request({
      url: `${apiPrefix}page`,
      data: {
        limit,
        page,
        status: true,
      },
      success(res) {
        const data = res.data || {};
        const list = data.data || [];

        const total = data.total || 0;
        that.setData({
          total,
        });

        let allList = that.data.allList || [];
        if (page > 1) {
          allList.push(...list);
        } else {
          allList = list;
        }

        that.setData({
          allList,
        });
      },
      complete() {
        wx.hideLoading();
      },
    });
  },

  // 详情页
  toDetail: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/detail/index?id=${id}`,
    });
  },

  // 滑动分页
  onScrolltolower(e) {
    const { direction } = e.detail;

    const { page = 1, total, limit } = this.data;

    const isMore = limit * page < total;

    this.setData({
      isMore,
    });

    console.log("onScrolltolower", direction, isMore);

    if (isMore && direction === "bottom") {
      this.getList({ page: page + 1 });
    }
  },
  onScroll(e) {
    // console.log("e", e);
    // const { scrollTop = 0 } = e.detail;
    // this.setData({
    //   dialogStyls: `top: ${scrollTop + 20}px !important;z-index: 9999;`,
    // });
  },

  // 打开弹窗
  onDialogOpen() {
    this.setData({
      concactShow: true,
    });
  },

  // 关闭弹窗
  onDialogClose() {
    this.setData({
      concactShow: false,
    });
  },

  // 提交
  onDialogSubmit() {
    const that = this;
    const { name, phone, content } = this.data;

    if (!name) {
      wx.showToast({
        title: "请输入姓名",
        icon: "error",
      });
      return;
    }

    if (!phone) {
      wx.showToast({
        title: "请输入手机号",
        icon: "error",
      });
      return;
    }

    if (!regPhone.test(phone)) {
      wx.showToast({
        title: "手机号不正确",
        icon: "error",
      });
      return;
    }
    if (phone.length !== 11) {
      wx.showToast({
        title: "手机号不正确",
        icon: "error",
      });
      return;
    }

    if (!content) {
      wx.showToast({
        title: "请输入需求",
        icon: "error",
      });
      return;
    }

    wx.showLoading();
    wx.request({
      url: `${apiPrefix}cooperate`,
      method: "POST",
      data: {
        name,
        phone,
        content,
      },
      success() {
        wx.showToast({
          title: "提交成功",
          icon: "success",
          duration: 2000,
        });

        that.setData({
          concactShow: false,
        });
      },
    });
  },

  //监听input获得焦点
  inputFocus(e) {
    console.log("e", e);
    this.setData({ bottom: e.detail.height });
  },
  inputBlur() {
    this.setData({ bottom: 0 });
  },
});
